package com.study.web;

import com.study.utils.SysUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("login")
@Api(tags = "登录测试")
public class LoginController {
    public static final String SESSION_ID = "loginName";

    @GetMapping("login")
    public String login(String name, HttpSession session) {
        log.info("登录名{}", name);
        session.setAttribute(SESSION_ID, name);
        return "success";
    }

    @GetMapping("now")
    public String now(HttpSession session) {
        Object attribute = session.getAttribute(SESSION_ID);
        String loginName = "";
        if (!StringUtils.isEmpty(attribute)) {
            loginName = attribute.toString();
        }
        return SysUtils.getDateSpan() + loginName;
    }
}
